package service;

import mapper.BrandMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.InputStream;
import java.util.List;
//业务层
public class BrandService {
//    品牌查询
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public List<Brand> selectAll() {
//        1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
//        2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        3.执行查询
        List<Brand> brandList = brandMapper.selectAll();
//        4.sqlSession关闭
        sqlSession.close();
//        5.返回数据
        return brandList;
    }
//    品牌新增
    public void add(Brand brand){
//        1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
//        2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        3.执行新增
        brandMapper.add(brand);
//        4.提交事务
        sqlSession.commit();
//        5.sqlSession关闭
        sqlSession.close();
    }
//    单个查询
public Brand selectById(int id) {
//        1.开启会话
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    SqlSession sqlSession = factory.openSession();
//        2.获取Mapper
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        3.执行查询
    Brand brand = brandMapper.selectById(id);
//        4.sqlSession关闭
    sqlSession.close();
//        5.返回数据
    return brand;
}
//品牌修改

public void update(Brand brand){
//        1.开启会话
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    SqlSession sqlSession = factory.openSession();
//        2.获取Mapper
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        3.执行新增
    brandMapper.update(brand);
//        4.提交事务
    sqlSession.commit();
//        5.sqlSession关闭
    sqlSession.close();
}
    /*** 删除 * @param id */
    public void delete(int id){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4. 调用方法
        mapper.deleteById(id);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

    }
//    批量删除
public void deleteByIds() throws Exception {
//    // 0.传参数
    int[] ids ={3,6};
    //1、加载mybatis的核心配置文件，获取SqlSessionFactory
    String resource = "mybatis-config.xml";//相对路径，基于resources
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2.有了工厂对象，就要获取SqlSession对象，执行SQL语句。
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //3.执行sql获取UserMapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    // 批量删除数据
    brandMapper.deleteByIds(ids);
    // 提交事务
    sqlSession.commit();
    //5.释放资源
    sqlSession.close();
}
    //分页品牌查询
    public PageBean<Brand> selectAllPage(int curPage, int pageSize){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        PageBean<Brand> pageBean = new PageBean<Brand>();
//3.执行查询(做2个查询)
//【计算begin的索引】
        int beginIndex = (curPage -1 ) * pageSize;//偏移量
        List<Brand> brandList = brandMapper.selectAllPage(beginIndex ,pageSize);//所查
        int totalCount = brandMapper.selectCount();//总数据条数
//4.将数据封装为分页格式
        pageBean.setData(brandList);
        pageBean.setCurrentPage(curPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(totalCount);
// 计算： 总页数
        int totalPage = (int) Math.ceil( totalCount*1.0/pageSize );
        pageBean.setTotalPage(totalPage);
//5.sqlSession关闭
        sqlSession.close();
//6.返回数据
        return pageBean;
    }
}